###############################################################################
##########  PACKAGE:  api_loader.pm
##########  PURPOSE:  Functions to populate the apidata hash
###############################################################################
package api_loader;

use strict;
use warnings;

use Data::Dumper qw(Dumper);

use eve_const;
use api_utils;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
	load_account_characters
	load_account_accountstatus
	load_char_charactersheet
	load_accountbalances
	load_walletjournal
	load_eve_reftypes
	load_eve_errorlist
	load_eve_certificatetree
	load_eve_skilltree
	load_server_serverstatus
);

###############################################################################
##### LOAD ACCOUNT CHARACTERS LIST
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{$userid}{account}{characters}
###############################################################################
sub load_account_characters {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_account_characters");
	if (exists $xml->{error}) {
		fail("api_loader", "load_account_characters", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
	foreach my $i (keys(%{$xml->{result}{rowset}{row}})) {
		foreach my $j (keys(%{$xml->{result}{rowset}{row}{$i}})) {
			$tmp->{characters}{$i}{lc($j)} = $xml->{result}{rowset}{row}{$i}{$j};
		}
	}
	logger("--  --  END load_account_characters");
	return $tmp;
}

###############################################################################
##### LOAD ACCOUNT ACCOUNTSTATUS LIST
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{$userid}{account}{accountstatus}
###############################################################################
sub load_account_accountstatus {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_account_accountstatus");
	if (exists $xml->{error}) {
		fail("api_loader", "load_account_accountstatus", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil}  = $xml->{cachedUntil};
	$tmp->{currenttime}  = $xml->{currentTime};
	$tmp->{version}      = $xml->{version};
	$tmp->{paiduntil}    = $xml->{result}{paidUntil};
	$tmp->{createdate}   = $xml->{result}{createDate};
	$tmp->{logonminutes} = $xml->{result}{logonMinutes};
	$tmp->{logoncount}   = $xml->{result}{logonCount};
	$tmp->{userid}       = $xml->{result}{userID};
	logger("--  --  END load_account_accountstatus");
	return $tmp;
}

###############################################################################
##### LOAD CHARACTER CHARACTER SHEET LIST
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{$userid}{$charid}{char}{charactersheet}
###############################################################################
sub load_char_charactersheet {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_char_charactersheet");
	if (exists $xml->{error}) {
		fail("api_loader", "load_char_charactersheet", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil}        = $xml->{cachedUntil};
	$tmp->{currenttime}        = $xml->{currentTime};
	$tmp->{version}            = $xml->{version};
	$tmp->{alliancename}       = $xml->{result}{allianceName};
	$tmp->{dob}                = $xml->{result}{DoB};
	$tmp->{cloneskillpoints}   = $xml->{result}{cloneSkillPoints};
	$tmp->{race}               = $xml->{result}{race};
	$tmp->{gender}             = $xml->{result}{gender};
	$tmp->{clonename}          = $xml->{result}{cloneName};
	$tmp->{characterid}        = $xml->{result}{characterID};
	$tmp->{bloodline}          = $xml->{result}{bloodLine};
	$tmp->{name}               = $xml->{result}{name};
	$tmp->{corporationid}      = $xml->{result}{corporationID};
	$tmp->{ancestry}           = $xml->{result}{ancestry};
	$tmp->{corporationname}    = $xml->{result}{corporationName};
	$tmp->{balance}            = $xml->{result}{balance};
	$tmp->{allianceid}         = $xml->{result}{allianceID};
	$tmp->{attributes}         = $xml->{result}{attributes};
	$tmp->{attributeenhancers} = $xml->{result}{attributeEnhancers};
	my $i = 0;
	for ($i=0; $i<=$#{$xml->{result}{rowset}{corporationTitles}{row}}; $i++) {
		$tmp->{corporationtitles}{$xml->{result}{rowset}{corporationTitles}{row}[$i]{titleID}} = $xml->{result}{rowset}{corporationTitles}{row}[$i]{titleName};
	}
	for ($i=0; $i<=$#{$xml->{result}{rowset}{corporationRoles}{row}}; $i++) {
		$tmp->{corporationroles}{$xml->{result}{rowset}{corporationRoles}{row}[$i]{roleID}} = $xml->{result}{rowset}{corporationRoles}{row}[$i]{roleName};
	}
	for ($i=0; $i<=$#{$xml->{result}{rowset}{skills}{row}}; $i++) {
		$tmp->{skills}{$xml->{result}{rowset}{skills}{row}[$i]{typeID}}{published}   = $xml->{result}{rowset}{skills}{row}[$i]{published};
		$tmp->{skills}{$xml->{result}{rowset}{skills}{row}[$i]{typeID}}{level}       = $xml->{result}{rowset}{skills}{row}[$i]{level};
		$tmp->{skills}{$xml->{result}{rowset}{skills}{row}[$i]{typeID}}{skillpoints} = $xml->{result}{rowset}{skills}{row}[$i]{skillpoints};
	}
	for ($i=0; $i<=$#{$xml->{result}{rowset}{corporationRolesAtOther}{row}}; $i++) {
		$tmp->{corporationrolesatother}{$xml->{result}{rowset}{corporationRolesAtOther}{row}[$i]{roleID}} = $xml->{result}{rowset}{corporationRolesAtOther}{row}[$i]{roleName};
	}
	for ($i=0; $i<=$#{$xml->{result}{rowset}{corporationRolesAtBase}{row}}; $i++) {
		$tmp->{corporationrolesatbase}{$xml->{result}{rowset}{corporationRolesAtBase}{row}[$i]{roleID}} = $xml->{result}{rowset}{corporationRolesAtBase}{row}[$i]{roleName};
	}
	for ($i=0; $i<=$#{$xml->{result}{rowset}{certificates}{row}}; $i++) {
		$tmp->{certificates}{$xml->{result}{rowset}{certificates}{row}[$i]{certificateID}} = $xml->{result}{rowset}{certificates}{row}[$i]{certificateID};
	}
	for ($i=0; $i<=$#{$xml->{result}{rowset}{corporationRolesAtHQ}{row}}; $i++) {
		$tmp->{corporationrolesathq}{$xml->{result}{rowset}{corporationRolesAtHQ}{row}[$i]{roleID}} = $xml->{result}{rowset}{corporationRolesAtHQ}{row}[$i]{roleName};
	}
	logger("--  --  END load_char_charactersheet");
	return $tmp;
}

###############################################################################
##### LOAD ACCOUNT BALANCES LIST
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{$userid}{$charid}{$category}{accountbalances}
###############################################################################
sub load_accountbalances {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_accountbalances");
	if (exists $xml->{error}) {
		fail("api_loader", "load_accountbalances", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
	my $i=0;
	for ($i=0; $i<=$#{$xml->{result}{rowset}{row}}; $i++) {
		$tmp->{accountid}{$xml->{result}{rowset}{row}[$i]{accountID}}{accountkey} = $xml->{result}{rowset}{row}[$i]{accountKey};
		$tmp->{accountid}{$xml->{result}{rowset}{row}[$i]{accountID}}{balance}    = $xml->{result}{rowset}{row}[$i]{balance};
	}
	logger("--  --  END load_accountbalances");
	return $tmp;
}

###############################################################################
##### LOAD WALLET JOURNAL LIST
##### INPUTS:  $accountkey, %xml
##### OUTPUTS:  %apidata{$userid}{$charid}{$category}{walletjournal}
###############################################################################
sub load_walletjournal {
	my $accountkey = $_[0];
	my $xml        = $_[1];
	my $tmp;

	logger("--  --  BEGIN load_walletjournal");
	if (exists $xml->{error}) {
		fail("api_loader", "load_walletjournal", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
	my $i = 0;
	for ($i=0; $i<=$#{$xml->{result}{rowset}{row}}; $i++) {
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{reftypeid}     = $xml->{result}{rowset}{row}[$i]{refTypeID};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{ownerName1}    = $xml->{result}{rowset}{row}[$i]{ownerName1};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{date}          = $xml->{result}{rowset}{row}[$i]{date};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{reason}        = $xml->{result}{rowset}{row}[$i]{reason};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{taxAmount}     = $xml->{result}{rowset}{row}[$i]{taxAmount};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{taxReceiverID} = $xml->{result}{rowset}{row}[$i]{taxReceiverID};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{amount}        = $xml->{result}{rowset}{row}[$i]{amount};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{balance}       = $xml->{result}{rowset}{row}[$i]{balance};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{argID1}        = $xml->{result}{rowset}{row}[$i]{argID1};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{ownerID1}      = $xml->{result}{rowset}{row}[$i]{ownerID1};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{argName1}      = $xml->{result}{rowset}{row}[$i]{argName1};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{ownerID2}      = $xml->{result}{rowset}{row}[$i]{ownerID2};
		$tmp->{accountkey}{$accountkey}{entries}{$xml->{result}{rowset}{row}[$i]{refID}}{ownerName2}    = $xml->{result}{rowset}{row}[$i]{ownerName2};
	}
	logger("--  --  END load_walletjournal");
	return $tmp;
}

###############################################################################
##### LOAD EVE REFTYPES LIST
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{eve}{reftypes}
###############################################################################
sub load_eve_reftypes {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_eve_reftypes");
	if (exists $xml->{error}) {
		fail("api_loader", "load_eve_reftypes", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
	my $i=0;
	for ($i=0; $i<=$#{$xml->{result}{rowset}{row}}; $i++) {
		$tmp->{reftypeid}{$xml->{result}{rowset}{row}[$i]{refTypeID}} = $xml->{result}{rowset}{row}[$i]{refTypeName};
	}
	logger("--  --  END load_eve_reftypes");
	return $tmp;
}

###############################################################################
##### LOAD EVE ERROR LIST
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{eve}{errorlist}
###############################################################################
sub load_eve_errorlist {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_eve_errorlist");
	if (exists $xml->{error}) {
		fail("api_loader", "load_eve_errorlist", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
	my $i=0;
	for ($i=0; $i<=$#{$xml->{result}{rowset}{row}}; $i++) {
		$tmp->{errorcode}{$xml->{result}{rowset}{row}[$i]{errorCode}} = $xml->{result}{rowset}{row}[$i]{errorText};
	}
	logger("--  --  END load_eve_errorlist");
	return $tmp;
}

###############################################################################
##### LOAD EVE CERTIFICATE TREE
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{eve}{certificatetree}
###############################################################################
sub load_eve_certificatetree {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_eve_certificatetree");
	if (exists $xml->{error}) {
		fail("api_loader", "load_eve_certificatetree", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
	my $i=0;
	for ($i=0; $i<=$#{$xml->{result}{rowset}{row}}; $i++) {
		$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{categoryname} = $xml->{result}{rowset}{row}[$i]{categoryName};
		my $j=0;
		for ($j=0; $j<=$#{$xml->{result}{rowset}{row}[$i]{rowset}{row}}; $j++) {
			$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{classname} = $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{className};
			my $k=0;
			for ($k=0; $k<=$#{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}}; $k++) {
				$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{grade} = $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{grade};
				$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{corporationID} = $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{corporationID};
				$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{description} = $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{description};
#				if (exists $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}) {
#					my $m=0;
#					for ($m=0; $m<=$#{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}}; $m++) {
#						$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{requiredcertificates}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}[$m]{certificateID}} = $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}[$m]{grade};
#					}
#				} else {
#					$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{requiredcertificates} = ' ';
#				}
#				if (exists $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}) {
#					my $m=0;
#					for ($m=0; $m<=$#{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}}; $m++) {
#						$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{requiredcertificates}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}[$m]{certificateID}} = $xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{rowset}{row}[$m]{grade};
#					}
#				} else {
#					$tmp->{categoryid}{$xml->{result}{rowset}{row}[$i]{categoryID}}{classid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{classID}}{certificateid}{$xml->{result}{rowset}{row}[$i]{rowset}{row}[$j]{rowset}{row}[$k]{certificateID}}{requiredcertificates} = ' ';
#				}

			}
		}
	}
	logger("--  --  END load_eve_certificatetree");
	return $tmp;
}

###############################################################################
##### LOAD EVE SKILL TREE
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{eve}{skilltree}
###############################################################################
sub load_eve_skilltree {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_eve_skilltree");
	if (exists $xml->{error}) {
		fail("api_loader", "load_eve_skilltree", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil} = $xml->{cachedUntil};
	$tmp->{currenttime} = $xml->{currentTime};
	$tmp->{version}     = $xml->{version};
#	my $i=0;
#	for ($i=0; $i<=$#{$xml->{result}{rowset}{row}}; $i++) {
#		$tmp->{errorcode}{$xml->{result}{rowset}{row}[$i]{errorCode}} = $xml->{result}{rowset}{row}[$i]{errorText};
#	}
	logger("--  --  END load_eve_skilltree");
	return $tmp;
}

###############################################################################
##### LOAD SERVER SERVER STATUS
##### INPUTS:  %xml
##### OUTPUTS:  %apidata{server}{serverstatus}
###############################################################################
sub load_server_serverstatus {
	my $xml = $_[0];
	my $tmp;

	logger("--  --  BEGIN load_server_serverstatus");
	if (exists $xml->{error}) {
		fail("api_loader", "load_server_serverstatus", "API ERROR: $xml->{error}{code}:  $xml->{error}{content}");
	}
	$tmp->{cacheduntil}   = $xml->{cachedUntil};
	$tmp->{currenttime}   = $xml->{currentTime};
	$tmp->{version}       = $xml->{version};
	$tmp->{serveropen}    = $xml->{result}{serverOpen};
	$tmp->{onlineplayers} = $xml->{result}{onlinePlayers};
	logger("--  --  END load_server_serverstatus");
	return $tmp;
}


1;
